import { Route } from '@/router/index'

export interface allRouter {
  path: string;
  name: string;
  component: any;
  redirect?: string | object,
  children: ChildRoute[];
  meta: RouteMeta;
}
interface RouteMeta {
  name: string;
  icon: string;
}
interface ChildRoute {
  path: string;
  name: string;
  component: any;
  meta: RouteMeta;
  children: ChildRoute[]
}

export function recursionRouter(userRouter: Array<allRouter> = [], allRouter: Array<allRouter> = []) {
  let realRoutes: Array<allRouter> = []; // 最终数据
  allRouter.forEach((v, i) => {
    userRouter.forEach((item, index) => {
        // 根据 name 对比
      if (item.name == v.meta.name) {
        // 比对子元素 递归
        if (item.children && item.children.length > 0) {
          v.children = recursionRouter(item.children, v.children);
        }
        realRoutes.push(v)
      }
    })
  })
  return realRoutes;
}

// routes 生成之后的路由
export function setDefaultRoute(routes: Array<Route>) {
  routes.forEach((v, i) => {
    if (v.children && v.children.length > 0) {
      v.redirect = { name: v.children[0].name }
      setDefaultRoute(v.children);
    }
  })
}